<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>文件下载器</title>
</head>
<body>
  <script type="text/javascript">
    // 创建读取流
    const readableStream = new ReadableStream({
      start(controller) {
        const textEncoder = new TextEncoder()
        const chunks = textEncoder.encode('这是一段数据', { stream: true })
        chunks.forEach(chunk => controller.enqueue(chunk))
        controller.close()
      }
    })
    // 创建写入流
    const writableStream = new WritableStream({
      write(chunk) {
        const textDecoder = new TextDecoder()
        return new Promise(resolve => {
          const buffer = new ArrayBuffer(2);
          const view = new Uint16Array(buffer);
          view[0] = chunk;
          const decoded = textDecoder.decode(view, { stream: true });
          console.log('decoded', decoded)

          setTimeout(() => {
            resolve()
          }, 1000)
        });
      },
      close() {
        console.log('writable stream close')
      },
    })

    readableStream.pipeTo(writableStream)
  </script>
</body>
</html>